/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.platform.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.gruul.common.core.constant.CommonConstants;
import com.medusa.gruul.platform.api.entity.AgentRule;
import com.medusa.gruul.platform.mapper.AgentRuleMapper;
import com.medusa.gruul.platform.model.dto.agent.AgentRuleDto;
import com.medusa.gruul.platform.service.IAgentRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 代理目标 服务实现类
 * </p>
 *
 * @author whh
 * @since 2020-10-04
 */
@Service
public class AgentRuleServiceImpl extends ServiceImpl<AgentRuleMapper, AgentRule> implements IAgentRuleService {

    @Override
    public void newAgentRule(List<AgentRuleDto> ruleDtos, Long agenInfoId, Integer ruleClass) {
        Map<Integer, AgentRuleDto> dtoMap = new HashMap<>(2);
        if (CollectionUtil.isNotEmpty(ruleDtos)) {
            dtoMap = ruleDtos.stream().collect(Collectors.toMap(AgentRuleDto::getRuleType, v -> v));
        }
        //代理目标或升级条件  1.销售总单数 2.销售总额
        //生成销售总单数目标或条件
        AgentRule agentRule = new AgentRule();
        agentRule.setRuleType(CommonConstants.NUMBER_ONE);
        agentRule.setAgentInfoId(agenInfoId);
        agentRule.setRuleClass(ruleClass);
        agentRule.setUseRule(CommonConstants.NUMBER_ZERO);
        agentRule.setTargetTime(CommonConstants.NUMBER_ZERO);
        agentRule.setTargetValue("0");
        AgentRuleDto agentRuleDto = dtoMap.get(CommonConstants.NUMBER_ONE);
        if (agentRuleDto != null) {
            agentRule.setTargetTime(agentRuleDto.getTargetTime());
            agentRule.setTargetValue(agentRuleDto.getTargetValue());
            agentRule.setUseRule(agentRuleDto.getUseRule() == null ? CommonConstants.NUMBER_ZERO : agentRuleDto.getUseRule());
        }
        this.save(agentRule);
        //生成销售总额目标或条件
        AgentRule agentRule2 = new AgentRule();
        agentRule2.setRuleType(CommonConstants.NUMBER_TWO);
        agentRule2.setAgentInfoId(agenInfoId);
        agentRule2.setRuleClass(ruleClass);
        agentRule2.setUseRule(CommonConstants.NUMBER_ZERO);
        agentRule2.setTargetTime(CommonConstants.NUMBER_ZERO);
        agentRule2.setTargetValue("0");
        AgentRuleDto agentRuleDto2 = dtoMap.get(CommonConstants.NUMBER_TWO);
        if (agentRuleDto2 != null) {
            agentRule2.setTargetTime(agentRuleDto2.getTargetTime());
            agentRule2.setTargetValue(agentRuleDto2.getTargetValue());
            agentRule2.setUseRule(agentRuleDto2.getUseRule() == null ? CommonConstants.NUMBER_ZERO : agentRuleDto2.getUseRule());
        }
        this.save(agentRule2);
    }

    @Override
    public void updateRule(Long agentInfoId, List<AgentRuleDto> ruleDtos, Integer ruleClass) {
        if (CollectionUtil.isEmpty(ruleDtos)) {
            return;
        }
        List<AgentRule> rules = this.getByAgenInfoIdAndRuleClass(agentInfoId, ruleClass);
        Map<Integer, AgentRuleDto> ruleTypeMap = ruleDtos.stream().collect(Collectors.toMap(AgentRuleDto::getRuleType, v -> v));
        for (AgentRule rule : rules) {
            AgentRuleDto ruleDto = ruleTypeMap.get(rule.getRuleType());
            if (ObjectUtil.isNotNull(ruleDto)) {
                BeanUtils.copyProperties(ruleDto, rule);
                this.updateById(rule);
            }
        }
    }

    @Override
    public List<AgentRule> getByAgentId(Long agentId) {
        return this.getBaseMapper().selectList(new QueryWrapper<AgentRule>()
                .eq("agent_info_id", agentId));
    }


    @Override
    public List<AgentRule> getByAgenInfoIdAndRuleClass(Long agentInfoId, Integer ruleClass) {
        return this.getBaseMapper().selectList(new QueryWrapper<AgentRule>().eq("agent_info_id", agentInfoId).eq("rule_class", ruleClass));
    }
}
